#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# Author    : zhenggaosong <2297145332@qq.com>, wuxiangyi
# Datetime  : 2022-05-14 10:52:37
# Product   : PyCharm
# Project   : Jupyter Notebook
# File      : ExcelParse.py
# explain   : 解析Excel

import datetime
import json
from xmlrpc.client import Boolean
import pandas as pd
import sys
import codecs


# 日期编码器
class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime("%Y-%m-%d %H:%M:%S")
        else:
            return json.JSONEncoder.default(self, obj)


# 获取sheet有效数据行数
def countColumns(df, headNum):
    flag = bool
    for index, row in df.iterrows():
        if index < headNum - 1:
            continue
        for i in range(len(row)):
            flag = False
            if pd.isna(row[i]):
                pass
            else:
                flag = True
        if flag == False:
            return index - 1


def main(filePath, sheetName, headNum):
    '''
    解析Excel表格
    Parameters
    ----------
    filePath：Excel文件路径
    sheetName：Excel中待解析的单元簿
    headNum：表头的行数

    Returns
    -------
        一个二维Array的数组，每一行是一个Array，每一列从左往右依次添加
    '''
    # 写入报表流
    df = pd.read_excel(filePath, sheet_name=sheetName)
    # 结果集
    result = []

    for index, row in df.iterrows():
        # 跳过表头
        if index < headNum - 1:
            continue
        obj = []
        # 获取解析列
        parseCol = len(row)
        # 录入数据
        for i in range(parseCol):
            # if pd.isna(row[i]):
            #     obj[i] = 'empty'
            # else:
            #     obj[i] = row[i]
            obj.append(row[i])
        result.append(obj)
    return result
    # print(json.dumps(result, ensure_ascii=False, cls=DateEncoder))


if __name__ == "__main__":
    # 文件路径
    filePath = sys.argv[1]
    # sheet名称
    sheetName = sys.argv[2]
    # 表头行数
    headNum = int(sys.argv[3])
    main(filePath, sheetName, headNum)
